package com.telecomyt.jwportal.activity.conversation.adapter;

import android.provider.CallLog;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.conversation.bean.CallInfoBean;

import java.text.SimpleDateFormat;

/**
 * @author created by chengDa
 * @time 2020/9/9  11:05
 * @description
 */
public class ConversationDetailsAdapter extends BaseQuickAdapter<CallInfoBean, BaseViewHolder> {
    public ConversationDetailsAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, CallInfoBean item) {
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy年MM月dd日 hh:mm");
        String dateStr = format.format(item.date);
        helper.setText(R.id.mTvTime, dateStr);
        TextView mTvStutas = helper.getView(R.id.mTvStutas);
        TextView mTvDuration = helper.getView(R.id.mTvDuration);
        int duration = item.getDuration();
        switch (item.getType()) {
            case CallLog.Calls.INCOMING_TYPE:
                //"打入"
                mTvStutas.setText("呼入");
                mTvDuration.setText(String.format("呼入%s", cal(duration)));
                mTvDuration.setTextColor(mContext.getResources().getColor(R.color.grayAC));
                break;
            case CallLog.Calls.OUTGOING_TYPE:
                //"打出"
                mTvStutas.setText("呼出");
                mTvDuration.setText(String.format("呼出%s", cal(duration)));
                mTvDuration.setTextColor(mContext.getResources().getColor(R.color.grayAC));
                break;
            case CallLog.Calls.MISSED_TYPE:
                mTvStutas.setText("");
                mTvDuration.setText("未接通");
                mTvDuration.setTextColor(mContext.getResources().getColor(R.color.red045));
                //"未接"
                break;
            default:
                break;
        }

    }

    public static String cal(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        return h + "时" + d + "分" + s + "秒";
    }
}
